﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Workbench.Persistance
{
    public class RepositoryBase
    {
        protected Func<SQLiteConnection> connectionProvider = null;

        internal RepositoryBase(Func<SQLiteConnection> connectionProvider)
        {
            this.connectionProvider = connectionProvider;
        }

        #region Repository Housekeeping

        protected SQLiteTransaction transaction;
        protected SQLiteConnection connection;

        public virtual void BeginTransaction()
        {
            if (transaction != null)
                transaction.Dispose();

            transaction = connection.BeginTransaction();
        }

        public virtual void Commit()
        {
            transaction.Commit();
        }

        public virtual void Rollback()
        {
            transaction.Rollback();
        }

        #endregion Repository Housekeeping

        #region IDisposable members

        public virtual void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (transaction != null)
                {
                    transaction.Dispose();
                    transaction = null;
                }
            }
        }

        #endregion
    }
}
